<template>
  <div>
    <p class="pinformation">样品统计</p>
    <el-form :model="form" ref="formRef" label-width="80">
      <el-row>
        <el-form-item label="样品编号×检测项目总数：" label-width="170">
          {{ form.sampleCodeCountJcxm }}
        </el-form-item>
        <el-form-item style="margin-left: 10px" label="已填报数：">
          {{ form.writeCount }}
        </el-form-item>
        <el-form-item style="margin-left: 10px" label="正在填报数：">
          {{ form.writeingCount }}
        </el-form-item>
        <el-form-item style="margin-left: 10px" label="未填报数：">
          {{ form.notWriteCount }}
        </el-form-item>
      </el-row>
    </el-form>
    <p class="pinformation">样品搜索</p>
    <el-form ref="queryRef" label-width="80" :model="queryParams" :inline="true">
      <el-form-item label="方法编号：" prop="methodCode" label-width="80">
        <el-input
          @keyup.enter="handleQuery"
          clearable
          maxlength="100"
          placeholder="请输入"
          v-model="queryParams.methodCode"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="样品编号：" prop="sampleCode" label-width="80">
        <el-input
          @keyup.enter="handleQuery"
          clearable
          maxlength="100"
          placeholder="请输入"
          v-model="queryParams.sampleCode"
        />
      </el-form-item>
      <el-form-item label="检测项目：" prop="jcxmName" label-width="80">
        <el-input
          @keyup.enter="handleQuery"
          clearable
          maxlength="100"
          placeholder="请输入检测项目"
          v-model="queryParams.jcxmName"
          style="width: 120px"
        />
      </el-form-item>
      <el-form-item label="状态：" prop="status" label-width="50">
        <el-select style="width: 100px" clearable filterable v-model="queryParams.status">
          <el-option value="-1" label="不限"></el-option>
          <el-option value="0" label="未填报"></el-option>
          <el-option value="1" label="填报中"></el-option>
          <el-option value="2" label="填报完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label-width="10" label=" ">
        <el-radio label="0" v-model="queryParams.sourceId">现场</el-radio>
        <el-radio label="1" v-model="queryParams.sourceId">实验室</el-radio>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="handleQuery">搜索</el-button>
        <el-button class="resetBtn" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <p class="pinformation">样品列表</p>
      <p class="totalStyle">共 {{ total }} 条数据</p>
    </el-row>
    <el-table border :data="tableData" ref="dataTableRef" v-loading="loading">
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="样品编号"
        prop="sampleCode"
        width="160"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="检测项目"
        width="100"
        >
        <template #default="scope">
          <span v-html="proxy.getSupAndSub(scope.row.jcxmName)"></span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="方法编号"
        prop="methodCode"
        width="130"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="方法名称"
        >
        <template #default="scope">
          <span v-html="proxy.getSupAndSub(scope.row.methodName)"></span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" show-overflow-tooltip label="交接时间" prop="withDate" width="140" /> -->
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="领取时间"
        prop="getTime"
        width="140"
      >
      <template #default="scope">
      {{ scope.row.getTime ? scope.row.getTime : ( scope.row.lMId ? '/' : '' )}}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="分析组"
        prop="userGroupName"
        width="90"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="分析人"
        prop="writeUserName"
        width="80"
        >
      <template #default="scope">
      {{ scope.row.status != 0 ?  scope.row.writeUserName : '' }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="分析时间"
        prop="analysisDate"
        width="100"
      >
      <template #default="scope">
      {{ scope.row.status != 0 ?  scope.row.analysisDate : '' }}
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="检测值" width="100">
        <template #default="scope">
          <span v-if="scope.row.avgValue">{{ scope.row.avgValue }}</span>
          <span v-else>{{ scope.row.resultValue }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="检出限" width="110">
        <template #default="scope">
          <span>{{ scope.row.checkLimitValue }}</span>
          <span v-html="proxy.getSupAndSub(scope.row.checkLimitUnit)"></span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" width="110">
        <template #default="scope">
          <span>{{ !scope.row.stepId && typeof scope.row.stepId != 'number'? '未填报' : ( scope.row.stepId == 0 ? '填报中' : '填报完成') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="操作"
        align="center"
        width="70"
        fixed="right"
      >
        <template #default="scope">
          <el-button
            link
            type="success"
            @click="handleWrite(scope.row)"
            v-show="scope.row.analysisDate"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <newBuiltTab ref="writeRef"></newBuiltTab>
    <el-dialog
      :class="'el-dialog-theme3'"
      v-model="localShow"
      title="查看原始记录表详情"
      fullscreen
    >
      <formList ref="formListRef" />
    </el-dialog>
  </div>
</template>

<script setup>
import newBuiltTab from "@/views/lab/labMethod/newBuilt.vue";
import { getFxypdFrom, getFxypdList } from "@/api/taskProgressDetails/index";
import formList from "@/views/cyCheck/public/common/formList";

const { proxy } = getCurrentInstance();
const tableData = ref([]);
const loading = ref(false);
const total = ref(0);
const queryParams = ref({ pageNum: 1, pageSize: 10, status: "-1", sourceId: "1" });
const form = ref({});
const writeRef = ref(null);
const projectId = ref("");
const localShow = ref(false);
const formListRef = ref(null);
function getForm(val) {
  projectId.value = val;
  queryParams.value.projectId = val;
  getFxypdFrom(projectId.value).then((res) => {
    form.value = res.data;
  });
  getList();
}

function getList() {
  loading.value = true;
  getFxypdList(queryParams.value).then((res) => {
    tableData.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleWrite(row) {
  if (typeof row.sourceId == "number" && row.sourceId == 0) {
    //查看现场表
    localShow.value = true;
    nextTick(() => {
      formListRef.value.getInfo(row.lMId);
    });
  } else {
    writeRef.value.open = true;
    writeRef.value.title = "数据详情";
    writeRef.value.lMId = row.lMId;
    writeRef.value.myId = row.lMId;
    nextTick(() => {
      writeRef.value.tabsView();
    });
  }
}

defineExpose({
  getForm,
});

const heightParams = ref([{ tableName: "data", bottomHeight: 21 }]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});

onUpdated(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "data", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "data", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);
</script>

<style></style>
